home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectDraw / SpriteAnimate / spriteanimate.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  17.0 KB  |  533 lines

  1. //-----------------------------------------------------------------------------
  2. // File: SpriteAnimate.cpp
  3. //
  4. // Desc: This sample demonstrates how to animate sprites using
  5. //       DirectDraw.  The samples runs in full-screen mode.  Pressing any
  6. //       key will exit the sample.
  7. //
  8. // Copyright (c) 1999-2001 Microsoft Corporation. All rights reserved.
  9. //-----------------------------------------------------------------------------
  10. #define STRICT
  11. #include <windows.h>
  12. #include <ddraw.h>
  13. #include <mmsystem.h>
  14. #include "resource.h"
  15. #include "ddutil.h"
  16.  
  17.  
  18.  
  19.  
  20. //-----------------------------------------------------------------------------
  21. // Defines, constants, and global variables
  22. //-----------------------------------------------------------------------------
  23. #define SAFE_DELETE(p)  { if(p) { delete (p);     (p)=NULL; } }
  24. #define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
  25.  
  26. #define SCREEN_WIDTH    640
  27. #define SCREEN_HEIGHT   480
  28. #define SCREEN_BPP      16
  29.  
  30. #define SPRITE_DIAMETER     48
  31. #define NUM_SPRITES         25
  32. #define NUM_FRAMES          30
  33. #define NUM_RAND            100
  34.  
  35. struct SPRITE_STRUCT
  36. {
  37.     FLOAT fRotationSpeed;
  38.     FLOAT fRotationTick;
  39.     LONG  lFrame;
  40.     BOOL  bClockwise;
  41.     FLOAT fPosX; 
  42.     FLOAT fPosY;
  43.     FLOAT fVelX; 
  44.     FLOAT fVelY;
  45. };
  46.  
  47. CDisplay*            g_pDisplay          = NULL;
  48. CSurface*            g_pAnimationSurface = NULL;  
  49. RECT                 g_rcViewport;          
  50. RECT                 g_rcScreen;            
  51. BOOL                 g_bActive     = FALSE; 
  52. DWORD                g_dwLastTick;
  53. SPRITE_STRUCT        g_Sprite[NUM_SPRITES]; 
  54. RECT                 g_rcFrame[NUM_FRAMES];
  55. LONG                 g_lRandTable[NUM_RAND];
  56. DWORD                g_dwRandIndex;
  57.  
  58.  
  59.  
  60.  
  61. //-----------------------------------------------------------------------------
  62. // Function-prototypes
  63. //-----------------------------------------------------------------------------
  64. LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
  65. HRESULT WinInit( HINSTANCE hInst, int nCmdShow, HWND* phWnd );
  66. HRESULT InitDirectDraw( HWND hWnd );
  67. VOID    FreeDirectDraw();
  68. HRESULT ProcessNextFrame();
  69. VOID    UpdateSprite( SPRITE_STRUCT* pSprite, FLOAT fTimeDelta );
  70. HRESULT DisplayFrame();
  71. HRESULT RestoreSurfaces();
  72.  
  73.  
  74.  
  75.  
  76. //-----------------------------------------------------------------------------
  77. // Name: WinMain()
  78. // Desc: Entry point to the program. Initializes everything and calls
  79. //       UpdateFrame() when idle from the message pump.
  80. //-----------------------------------------------------------------------------
  81. int APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, int nCmdShow )
  82. {
  83.     MSG         msg;
  84.     HWND     hWnd;
  85.  
  86.     ZeroMemory( &g_Sprite, sizeof(SPRITE_STRUCT) * NUM_SPRITES );
  87.     srand( GetTickCount() );
  88.  
  89.     if( FAILED( WinInit( hInst, nCmdShow, &hWnd ) ) )
  90.         return FALSE;
  91.  
  92.     // Make a timer go off to re-init the table of random values every once in a while
  93.     SetTimer( hWnd, 0, 1500, NULL );
  94.  
  95.     if( FAILED( InitDirectDraw( hWnd ) ) )
  96.     {
  97.         if( g_pDisplay )
  98.             g_pDisplay->GetDirectDraw()->SetCooperativeLevel( NULL, DDSCL_NORMAL );
  99.  
  100.         MessageBox( hWnd, TEXT("DirectDraw init failed. ")
  101.                     TEXT("The sample will now exit. "), TEXT("DirectDraw Sample"), 
  102.                     MB_ICONERROR | MB_OK );
  103.         return FALSE;
  104.     }
  105.  
  106.     g_dwLastTick = timeGetTime();
  107.  
  108.     while( TRUE )
  109.     {
  110.         // Look for messages, if none are found then 
  111.         // update the state and display it
  112.         if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
  113.         {
  114.             if( 0 == GetMessage(&msg, NULL, 0, 0 ) )
  115.             {
  116.                 // WM_QUIT was posted, so exit
  117.                 return (int)msg.wParam;
  118.             }
  119.  
  120.             // Translate and dispatch the message
  121.             TranslateMessage( &msg ); 
  122.             DispatchMessage( &msg );
  123.         }
  124.         else
  125.         {
  126.             if( g_bActive )
  127.             {
  128.                 // Move the sprites, blt them to the back buffer, then 
  129.                 // flip or blt the back buffer to the primary buffer
  130.                 if( FAILED( ProcessNextFrame() ) )
  131.                 {
  132.                     SAFE_DELETE( g_pDisplay );
  133.  
  134.                     MessageBox( hWnd, TEXT("Displaying the next frame failed. ")
  135.                                 TEXT("The sample will now exit. "), TEXT("DirectDraw Sample"), 
  136.                                 MB_ICONERROR | MB_OK );
  137.                     return FALSE;
  138.                 }
  139.             }
  140.             else
  141.             {
  142.                 // Make sure we go to sleep if we have nothing else to do
  143.                 WaitMessage();
  144.  
  145.                 // Ignore time spent inactive 
  146.                 g_dwLastTick = timeGetTime();
  147.             }
  148.         }
  149.     }
  150. }
  151.  
  152.  
  153.  
  154.  
  155. //-----------------------------------------------------------------------------
  156. // Name: WinInit()
  157. // Desc: Init the window
  158. //-----------------------------------------------------------------------------
  159. HRESULT WinInit( HINSTANCE hInst, int nCmdShow, HWND* phWnd )
  160. {
  161.     WNDCLASS wc;
  162.     HWND     hWnd;
  163.  
  164.     // Register the Window Class
  165.     wc.lpszClassName = TEXT("SpriteAnimate");
  166.     wc.lpfnWndProc   = MainWndProc;
  167.     wc.style         = CS_VREDRAW | CS_HREDRAW;
  168.     wc.hInstance     = hInst;
  169.     wc.hIcon         = LoadIcon( hInst, MAKEINTRESOURCE(IDI_MAIN) );
  170.     wc.hCursor       = LoadCursor( NULL, IDC_ARROW );
  171.     wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
  172.     wc.lpszMenuName  = NULL;
  173.     wc.cbClsExtra    = 0;
  174.     wc.cbWndExtra    = 0;
  175.  
  176.     if( RegisterClass( &wc ) == 0 )
  177.         return E_FAIL;
  178.  
  179.     // Create and show the main window
  180.     hWnd = CreateWindowEx( 0, TEXT("SpriteAnimate"), TEXT("DirectDraw SpriteAnimate Sample"),
  181.                            WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT,
  182.                              CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInst, NULL );
  183.     if( hWnd == NULL )
  184.         return E_FAIL;
  185.  
  186.     ShowWindow( hWnd, nCmdShow );
  187.     UpdateWindow( hWnd );
  188.  
  189.     *phWnd = hWnd;
  190.  
  191.     return S_OK;
  192. }
  193.  
  194.  
  195.  
  196.  
  197. //-----------------------------------------------------------------------------
  198. // Name: InitDirectDraw()
  199. // Desc: Create the DirectDraw object, and init the surfaces
  200. //-----------------------------------------------------------------------------
  201. HRESULT InitDirectDraw( HWND hWnd )
  202. {
  203.     HRESULT             hr;
  204.  
  205.     g_pDisplay = new CDisplay();
  206.     if( FAILED( hr = g_pDisplay->CreateFullScreenDisplay( hWnd, SCREEN_WIDTH, 
  207.                                                           SCREEN_HEIGHT, SCREEN_BPP ) ) )
  208.     {
  209.         MessageBox( hWnd, TEXT("This display card does not support 640x480x16. "),
  210.                     TEXT("DirectDraw Sample"), MB_ICONERROR | MB_OK );
  211.         return hr;
  212.     }
  213.  
  214.     // Create a surface, and draw a bitmap resource on it.  
  215.     if( FAILED( hr = g_pDisplay->CreateSurfaceFromBitmap( &g_pAnimationSurface, 
  216.                                                           MAKEINTRESOURCE( IDB_ANIMATE_SHEET ), 
  217.                                                           SPRITE_DIAMETER * 5, 
  218.                                                           SPRITE_DIAMETER * 6 ) ) )        
  219.         return hr;
  220.  
  221.     // Set the color key for the logo sprite to black
  222.     if( FAILED( hr = g_pAnimationSurface->SetColorKey( 0 ) ) )
  223.         return hr;
  224.  
  225.     // Init all the sprites.  All of these sprites using the 
  226.     // same g_pDDSAnimationSheet surface, but depending on the
  227.     // sprite's lFrame value, it indexes a different rect on the 
  228.     // surface.
  229.     for( int iSprite = 0; iSprite < NUM_SPRITES; iSprite++ )
  230.     {
  231.         // Set the sprite's position and velocity
  232.         g_Sprite[iSprite].fPosX = (float) (rand() % SCREEN_WIDTH);
  233.         g_Sprite[iSprite].fPosY = (float) (rand() % SCREEN_HEIGHT); 
  234.  
  235.         g_Sprite[iSprite].fVelX = 500.0f * rand() / RAND_MAX - 250.0f;
  236.         g_Sprite[iSprite].fVelY = 500.0f * rand() / RAND_MAX - 250.0f;
  237.  
  238.         g_Sprite[iSprite].lFrame          = rand() % NUM_FRAMES;
  239.         g_Sprite[iSprite].fRotationTick   = 0.0f;
  240.         g_Sprite[iSprite].fRotationSpeed  = (rand() % 50 + 25 ) / 1000.0f; 
  241.         g_Sprite[iSprite].bClockwise      = rand() % 2;
  242.     }
  243.  
  244.     // Precompute the source rects for g_pDDSAnimateSheet.  The source rects 
  245.     // are used during the blt of the dds to the backbuffer. 
  246.     for( int iFrame = 0; iFrame < NUM_FRAMES; iFrame++ )
  247.     {
  248.         g_rcFrame[iFrame].top    = (iFrame / 5) * SPRITE_DIAMETER;
  249.         g_rcFrame[iFrame].left   = (iFrame % 5) * SPRITE_DIAMETER;
  250.  
  251.         g_rcFrame[iFrame].bottom = g_rcFrame[iFrame].top  + SPRITE_DIAMETER;
  252.         g_rcFrame[iFrame].right  = g_rcFrame[iFrame].left + SPRITE_DIAMETER;
  253.     }
  254.  
  255.     // Init a array of random values.  This array is used to create the
  256.     // 'flocking' effect the seen in the sample.
  257.     g_dwRandIndex = 0;
  258.     for( int iRand = 0; iRand < NUM_RAND; iRand++ )
  259.         g_lRandTable[ iRand ] = rand();    
  260.  
  261.     return S_OK;
  262. }
  263.  
  264.  
  265.  
  266.  
  267. //-----------------------------------------------------------------------------
  268. // Name: FreeDirectDraw()
  269. // Desc: Release all the DirectDraw objects
  270. //-----------------------------------------------------------------------------
  271. VOID FreeDirectDraw()
  272. {
  273.     SAFE_DELETE( g_pAnimationSurface );
  274.     SAFE_DELETE( g_pDisplay );
  275. }
  276.  
  277.  
  278.  
  279.  
  280. //-----------------------------------------------------------------------------
  281. // Name: MainWndProc()
  282. // Desc: The main window procedure
  283. //-----------------------------------------------------------------------------
  284. LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  285. {
  286.     switch (msg)
  287.     {
  288.         case WM_KEYDOWN:
  289.             PostMessage( hWnd, WM_CLOSE, 0, 0 );
  290.             return 0L;
  291.  
  292.         case WM_TIMER:
  293.             {
  294.                 // The timer triggered, so re-init table of random values
  295.                 g_dwRandIndex = 0;
  296.                 for( int iRand = 0; iRand < NUM_RAND; iRand++ )
  297.                     g_lRandTable[ iRand ] = rand();    
  298.             }
  299.             break;
  300.  
  301.         case WM_SETCURSOR:
  302.             // Hide the cursor in fullscreen 
  303.             SetCursor( NULL );
  304.             return TRUE;
  305.  
  306.         case WM_SIZE:
  307.             // Check to see if we are losing our window...
  308.             if( SIZE_MAXHIDE==wParam || SIZE_MINIMIZED==wParam )
  309.                 g_bActive = FALSE;
  310.             else
  311.                 g_bActive = TRUE;
  312.             break;
  313.  
  314.         case WM_EXITMENULOOP:
  315.             // Ignore time spent in menu
  316.             g_dwLastTick = timeGetTime();
  317.             break;
  318.  
  319.         case WM_EXITSIZEMOVE:
  320.             // Ignore time spent resizing
  321.             g_dwLastTick = timeGetTime();
  322.             break;
  323.             
  324.         case WM_SYSCOMMAND:
  325.             // Prevent moving/sizing and power loss in fullscreen mode
  326.             switch( wParam )
  327.             {
  328.                 case SC_MOVE:
  329.                 case SC_SIZE:
  330.                 case SC_MAXIMIZE:
  331.                 case SC_MONITORPOWER:
  332.                     return TRUE;
  333.             }
  334.             break;
  335.             
  336.         case WM_DESTROY:
  337.             // Cleanup and close the app
  338.             FreeDirectDraw();
  339.             PostQuitMessage( 0 );
  340.             return 0L;
  341.     }
  342.  
  343.     return DefWindowProc(hWnd, msg, wParam, lParam);
  344. }
  345.  
  346.  
  347.  
  348.  
  349. //-----------------------------------------------------------------------------
  350. // Name: ProcessNextFrame()
  351. // Desc: Move the sprites, blt them to the back buffer, then 
  352. //       flips the back buffer to the primary buffer
  353. //-----------------------------------------------------------------------------
  354. HRESULT ProcessNextFrame()
  355. {
  356.     HRESULT hr;
  357.  
  358.     // Figure how much time has passed since the last time
  359.     DWORD dwCurrTick = timeGetTime();
  360.     DWORD dwTickDiff = dwCurrTick - g_dwLastTick;
  361.  
  362.     // Don't update if no time has passed 
  363.     if( dwTickDiff == 0 )
  364.         return S_OK; 
  365.  
  366.     g_dwLastTick = dwCurrTick;
  367.  
  368.     // Update the sprites according to how much time has passed
  369.     for( int iSprite = 0; iSprite < NUM_SPRITES; iSprite++ )
  370.         UpdateSprite( &g_Sprite[ iSprite ], dwTickDiff / 1000.0f );
  371.  
  372.     // Display the sprites on the screen
  373.     if( FAILED( hr = DisplayFrame() ) )
  374.     {
  375.         if( hr != DDERR_SURFACELOST )
  376.             return hr;
  377.  
  378.         // The surfaces were lost so restore them 
  379.         RestoreSurfaces();
  380.     }
  381.  
  382.     return S_OK;
  383. }
  384.  
  385.  
  386.  
  387. //-----------------------------------------------------------------------------
  388. // Name: GetNextRand()
  389. // Desc: Gets the next element from the circular array of random values
  390. //-----------------------------------------------------------------------------
  391. LONG GetNextRand()
  392. {    
  393.     LONG lRand = g_lRandTable[ g_dwRandIndex++ ];
  394.     g_dwRandIndex %= NUM_RAND;
  395.  
  396.     return lRand;
  397. }
  398.  
  399.  
  400.  
  401. //-----------------------------------------------------------------------------
  402. // Name: UpdateSprite()
  403. // Desc: Moves and bounces the sprite based on how much time has passed.  
  404. //       It also changes the sprite state based on random values it gets 
  405. //       from the array g_lRandTable
  406. //-----------------------------------------------------------------------------
  407. VOID UpdateSprite( SPRITE_STRUCT* pSprite, FLOAT fTimeDelta )
  408. {    
  409.     // Update the sprite position
  410.     pSprite->fPosX += pSprite->fVelX * fTimeDelta;
  411.     pSprite->fPosY += pSprite->fVelY * fTimeDelta;
  412.  
  413.     // See if its time to advance the sprite to the next frame
  414.     pSprite->fRotationTick += fTimeDelta;
  415.     if( pSprite->fRotationTick > pSprite->fRotationSpeed )
  416.     {
  417.         // If it is, then either change the frame clockwise or counter-clockwise
  418.         if( pSprite->bClockwise )
  419.         {
  420.             pSprite->lFrame++;
  421.             pSprite->lFrame %= NUM_FRAMES; 
  422.         }
  423.         else
  424.         {
  425.             pSprite->lFrame--;
  426.             if( pSprite->lFrame < 0 )
  427.                 pSprite->lFrame = NUM_FRAMES - 1;
  428.         }
  429.  
  430.         pSprite->fRotationTick = 0;        
  431.     }
  432.  
  433.     // Using the next element from the random arry, 
  434.     // randomize the velocity of the sprite
  435.     if( GetNextRand() % 100 < 2 )
  436.     {
  437.         pSprite->fVelX = 500.0f * GetNextRand() / RAND_MAX - 250.0f;
  438.         pSprite->fVelY = 500.0f * GetNextRand() / RAND_MAX - 250.0f;
  439.     }
  440.  
  441.     // Using the next element from the random arry, 
  442.     // randomize the rotational speed of the sprite
  443.     if( GetNextRand() % 100 < 5 )
  444.         pSprite->fRotationSpeed = ( GetNextRand() % 50 + 5 ) / 1000.0f;
  445.  
  446.     // Clip the position, and bounce if it hits the edge
  447.     if( pSprite->fPosX < 0.0f )
  448.     {
  449.         pSprite->fPosX  = 0;
  450.         pSprite->fVelX = -pSprite->fVelX;
  451.     }
  452.  
  453.     if( pSprite->fPosX >= SCREEN_WIDTH - SPRITE_DIAMETER )
  454.     {
  455.         pSprite->fPosX = SCREEN_WIDTH - 1 - SPRITE_DIAMETER;
  456.         pSprite->fVelX = -pSprite->fVelX;
  457.     }
  458.  
  459.     if( pSprite->fPosY < 0 )
  460.     {
  461.         pSprite->fPosY = 0;
  462.         pSprite->fVelY = -pSprite->fVelY;
  463.     }
  464.  
  465.     if( pSprite->fPosY > SCREEN_HEIGHT - SPRITE_DIAMETER )
  466.     {
  467.         pSprite->fPosY = SCREEN_HEIGHT - 1 - SPRITE_DIAMETER;
  468.         pSprite->fVelY = -pSprite->fVelY;
  469.     }   
  470. }
  471.  
  472.  
  473.  
  474.  
  475. //-----------------------------------------------------------------------------
  476. // Name: DisplayFrame()
  477. // Desc: Blts a the sprites to the back buffer, then flips the 
  478. //       back buffer onto the primary buffer.
  479. //-----------------------------------------------------------------------------
  480. HRESULT DisplayFrame()
  481. {
  482.     HRESULT hr;
  483.  
  484.     // Fill the back buffer with black, ignoring errors until the flip
  485.     g_pDisplay->Clear( 0 );
  486.  
  487.     // Blt all the sprites onto the back buffer using color keying,
  488.     // ignoring errors until the flip. Note that all of these sprites 
  489.     // use the same DirectDraw surface.
  490.     for( int iSprite = 0; iSprite < NUM_SPRITES; iSprite++ )
  491.     {
  492.         g_pDisplay->Blt( (DWORD)g_Sprite[iSprite].fPosX, 
  493.                          (DWORD)g_Sprite[iSprite].fPosY, 
  494.                          g_pAnimationSurface, 
  495.                          &g_rcFrame[ g_Sprite[iSprite].lFrame ] );
  496.     }
  497.  
  498.     // We are in fullscreen mode, so perform a flip and return 
  499.     // any errors like DDERR_SURFACELOST
  500.     if( FAILED( hr = g_pDisplay->Present() ) )
  501.         return hr;
  502.  
  503.     return S_OK;
  504. }
  505.  
  506.  
  507.  
  508.  
  509. //-----------------------------------------------------------------------------
  510. // Name: RestoreSurfaces()
  511. // Desc: Restore all the surfaces, and redraw the sprite surfaces.
  512. //-----------------------------------------------------------------------------
  513. HRESULT RestoreSurfaces()
  514. {
  515.     HRESULT hr;
  516.  
  517.     if( FAILED( hr = g_pDisplay->GetDirectDraw()->RestoreAllSurfaces() ) )
  518.         return hr;
  519.  
  520.     // No need to re-create the surface, just re-draw it.
  521.     if( FAILED( hr = g_pAnimationSurface->DrawBitmap( MAKEINTRESOURCE( IDB_ANIMATE_SHEET ), 
  522.                                                       SPRITE_DIAMETER * 5, 
  523.                                                       SPRITE_DIAMETER * 6 ) ) )        
  524.         return hr;
  525.                                                           
  526.  
  527.     return S_OK;
  528. }
  529.  
  530.  
  531.  
  532.  
  533.